/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "roomitemswidget.h"
#include "ui_roomitemswidget.h"

#include "client/core/client.h"
#include "client/core/room.h"
#include "client/core/target.h"
#include "client/mva/roommodel.h"
#include "core/staticroomitem.h"

#include <QtGui/QTableView>

using namespace greenhills::client;
using namespace greenhills::client::core;

CRoomItemsWidget::CRoomItemsWidget(CClient &pClient, QWidget *parent)
    : QWidget(parent), mClient(pClient), m_ui(new Ui::CRoomItemsWidget)
{
    m_ui->setupUi(this);

    QObject::connect(m_ui->pushButtonDelete, SIGNAL(clicked()),
            this, SLOT(onDeleteButtonClicked()));
    QObject::connect(m_ui->pushButtonNavigate, SIGNAL(clicked()),
            this, SLOT(onNavigateButtonClicked()));

    QObject::connect(&mClient, SIGNAL(roomJoined()),
            this, SLOT(onRoomJoined()));
    QObject::connect(&mClient, SIGNAL(roomLeft()),
            this, SLOT(onRoomLeft()));
}

CRoomItemsWidget::~CRoomItemsWidget()
{
    delete m_ui;
}

void CRoomItemsWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CRoomItemsWidget::onRoomJoined()
{
    m_ui->tableView->setModel(mClient.room()->model());
}

void CRoomItemsWidget::onRoomLeft()
{
    m_ui->tableView->setModel(0);
}

void CRoomItemsWidget::onDeleteButtonClicked()
{
    greenhills::core::IRoomItem *roomItem = selectedItem();

    if(roomItem)
        mClient.removeItem(roomItem);
}

void CRoomItemsWidget::onNavigateButtonClicked()
{
    greenhills::core::IRoomItem *roomItem = selectedItem();

    if(roomItem == 0)
        return;

    greenhills::core::CStaticRoomItem *posRoomItem =
            dynamic_cast<greenhills::core::CStaticRoomItem*>(roomItem);
    if (posRoomItem == 0)
        return;

    CTarget::setPosition(posRoomItem->position());
}

greenhills::core::IRoomItem *CRoomItemsWidget::selectedItem()
{
    QModelIndex mi = m_ui->tableView->currentIndex();

    if (!mi.isValid())
        return 0;

    QModelIndex miId = mClient.room()->model()->index(
            mi.row(), greenhills::client::mva::CRoomModel::ColumnId);

    bool ok;
    greenhills::core::CItemId id =
            mClient.room()->model()->data( miId, Qt::DisplayRole).toInt(&ok);

    if (!ok)
        return 0;

    return mClient.room()->item(id);
}
